/* ----------------------------------------------------------------------------
 * Copyright (c) 2008 by Airbus SAS. All rights reserved. All intellectual
 * property rights in and/or in the computer program and its related
 * documentation and technology are the sole Airbus' property. This computer
 * program is under Airbus copyright and cannot be in whole or in part
 * reproduced, sublicensed, leased, sold or used in any form or by any means,
 * including without limitation graphic, electronic, mechanical, photocopying,
 * recording, taping or information storage and retrieval systems without Airbus
 * prior written consent. The downloading, exporting or reexporting of this
 * computer program or any related documentation or technology is subject to any
 * export rules, including US regulations.
 * ----------------------------------------------------------------------------
 */

package com.azzaou.util;

import java.io.Serializable;

import javax.faces.context.FacesContext;

import org.apache.log4j.Logger;


/**
 * TechnicalException
 * 
 * @version : V1.0
 * @author : moabdi
 * @Project : [AWN_PACAT_ENGINEERING_ADMINTOOLS]
 * @Date : juil. 2012
 * @DSL_Requirements :
 * @Product_Requirements :
 */
public class TechnicalException implements Serializable
{
  /**
   * Logger for this class
   */
  private static final Logger LOGGER = Logger
      .getLogger(TechnicalException.class);
 
  /**
   * serialVersionUID
   */
  private static final long serialVersionUID = 1L;

  /**
   * errDetails
   */
  private String errDetails;

  /**
   * cause
   */
  private String cause;

  /**
   * Exception
   */
  private Exception exception;
  
  /**
   * get Err Connection
   * 
   * @return String
   */
  public String getErrDetails()
  {
    String ex = "";
    if(exception != null && !"".equalsIgnoreCase(exception.toString()))
    {
      if(exception.getLocalizedMessage()!=null)
      {
        ex = exception.getLocalizedMessage().toString();
      }
      else
      {
        ex = exception.toString();
      }
    }
    errDetails = "This error occurred because of the following Exception : "+ ex  
      + "\n \n"
      + "We advise you to apply the "
      + " following procedure: consult admintools.log for more details or contact your administrator. ";

    return errDetails;
  }

  /**
   * setErrDetails
   * 
   * @param pErrDetails
   *          : pErrDetails
   */
  public void setErrDetails(String pErrDetails)
  {
     this.errDetails = pErrDetails;
  }

  /**getCause
   * 
   * @return the message
   */
  public String getCause()
  {
    try
    {
      if (exception != null && !"".equalsIgnoreCase(exception.toString()))
      {
        String[] exClass = exception.getClass().getName().split("\\.");
        String key = exClass[exClass.length - 1];
        String errCause = BuildMessage.getMessageResourceString(
            "i18n.technicalException", key, null, FacesContext
                .getCurrentInstance().getViewRoot().getLocale());
        if (errCause != null && !"".equalsIgnoreCase(errCause))
        {
          cause = errCause;
        }
        else
        {
          String localizedMessage = exception.getLocalizedMessage().toString();
          if (localizedMessage != null
              && !"".equalsIgnoreCase(localizedMessage))
          {
            cause = localizedMessage;
          }
          else
          {
            cause = "An unexpected error occurred on the server.";
          }
        }
      }
    }
    catch (Exception e)
    {
      LOGGER.error("An error occured : ", e);
    }
    return cause;
  }

  /**
   * @return exception
   */
  public Exception getException()
  {
    return exception;
  }

  /**
   * @param exception exception to define
   */
  public void setException(Exception exception)
  {
    this.exception = exception;
  }

}